//
//  YYUserAccount.swift
//  YYWeibo
//
//  Created by strongwallyi on 15/12/30.
//  Copyright © 2015年 strongwallyi. All rights reserved.
//

import UIKit

class YYUserAccount: NSObject {

    //用户头像
    var avatar_large:String?
    
    //用户昵称
    var screen_name:String?
    
    //用户的 uid
    var uid: String?
    
    //过期时间,从获取回来那一刻开始计算过期时间
    var expires_in: NSTimeInterval = 0{
        didSet{
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
        }
    }
    
    //过期时刻
    var expiresDate: NSDate?
    
    //访问令牌
    var access_token: String?
    
    //过期时间.很少用了
    var remind_in: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    //归档
    func encodeWithCoder(encoder:NSCoder){
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        encoder.encodeObject(screen_name, forKey: "screen_name")
        encoder.encodeObject(uid, forKey: "uid")
//        encoder.encodeObject(expires_in, forKey: "expires_in")
        encoder.encodeObject(expires_in, forKey: "expiresDate")
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(remind_in, forKey: "remind_in")
    }
    
    //解档
    required init?(coder decoder: NSCoder) {
        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String
        screen_name = decoder.decodeObjectForKey("screen_name") as? String
        uid = decoder.decodeObjectForKey("uid") as? String
//        expires_in = decoder.decodeIntegerForKey("expires_in") as? NSDate
        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate
        

        access_token = decoder.decodeObjectForKey("access_token") as? String
        remind_in = decoder.decodeObjectForKey("remind_in") as? String
    }

    //重写 description属性
    override var description: String {
        let keys = ["uid","expires_in","access_token","remind_in","avatar_large","screen_name"]
        return dictionaryWithValuesForKeys(keys).description
        
    }
    
    
}
